package com.ewzj.bigdata.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.di.component.DaggerRmRecommendComponent;
import com.ewzj.bigdata.di.module.RmRecommendModule;
import com.ewzj.bigdata.mvp.contract.RmRecommendContract;
import com.ewzj.bigdata.mvp.presenter.RmRecommendPresenter;
import com.ewzj.bigdata.mvp.ui.adapter.BannerAdapter;
import com.ewzj.bigdata.mvp.ui.widget.CardScaleHelper;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class RmRecommendFragment extends BaseFragment<RmRecommendPresenter> implements RmRecommendContract.View {


    @BindView(R.id.rv_banner)
    RecyclerView rvBanner;
    private CardScaleHelper mCardScaleHelper = null;
    public static RmRecommendFragment newInstance() {
        RmRecommendFragment fragment = new RmRecommendFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerRmRecommendComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .rmRecommendModule(new RmRecommendModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_rm_recommend, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initbanner();
    }

    private void initbanner() {
        List<String> mList=new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            mList.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2946550071,381041431&fm=11&gp=0.jpg");
            mList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1490984320392&di=8290126f83c2a2c0d45be41e3f88a6d0&imgtype=0&src=http%3A%2F%2Ffile.mumayi.com%2Fforum%2F201307%2F19%2F152440r9ov9ololkzdcz7d.jpg");
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        rvBanner.setLayoutManager(linearLayoutManager);

        rvBanner.setAdapter(new BannerAdapter(mList));
        // mRecyclerView绑定scale效果
        mCardScaleHelper = new CardScaleHelper();
        mCardScaleHelper.setCurrentItemPos(2);
        mCardScaleHelper.attachToRecyclerView(rvBanner);
//        autoScroll(rvBanner);
    }


    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

}
